﻿using System;
using System.Collections.Generic;
//
using PerceptiveMCAPI;
using PerceptiveMCAPI.Types;
//
namespace PerceptiveMCAPI_Test
{
   public partial class get_campaignCreate : System.Web.UI.UserControl
   {
      public ApiMethod method { get; set; }

      public string ListId
      {
         get { return tbListId.Text.Trim(); }
         set { tbListId.Text = value; }
      }
      public EnumValues.campaign_type campaignType
      {
         get { return ApiHelper.GetEnumFromString<EnumValues.campaign_type>( ddlCampaignType.SelectedItem.Text ); }
         set { ddlCampaignType.SelectedValue = Convert.ToInt32( value ).ToString(); }
      }

      public campaignCreateOptions options
      {
         get { return campaignOptionsFormat(); }
      }
      public Dictionary<string, string> content
      {
         get { return campaignContentFormat(); }
      }
      public campaignSegmentOptions segment_opts
      {
         get { return campaignSegmentOptsFormat(); }
      }
      public Dictionary<string, string> type_opts
      {
         get { return campaignTypeOptsFormat(); }
      }

      // ------------------------------------------------------------
      public void Setup()
      {
         this.Visible = true;
         lblSection.Text = method.section;
         lblMethod.Text = method.name;
         tbListId.Focus();
         //
         ddlCampaignType.DataSource = EnumValues.campaignTypeValues();
         ddlCampaignType.DataBind();
         ddlCampaignType.SelectedIndex = 1;
         //
         SetupDefaults();
      }

      protected void ddlCampaignType_SelectedIndexChanged( object sender, EventArgs e )
      {
         SetupDefaults();
         //
         if ( campaignType == EnumValues.campaign_type.regular || campaignType == EnumValues.campaign_type.trans )
         {
         }
         else if ( campaignType == EnumValues.campaign_type.plaintext )
         {
            divContentHtml.Visible = false;
         }
         else if ( campaignType == EnumValues.campaign_type.absplit )
         {
            divTypeOptsABSplit.Visible = true;
         }
         else if ( campaignType == EnumValues.campaign_type.rss )
         {
            divTypeOptsRss.Visible = true;
            sub_RssOptions1.Setup();
         }
         else if ( campaignType == EnumValues.campaign_type.auto )
         {
            divTypeOptsAuto.Visible = true;
         }
         else
         {
            // default to regular (inspect not supported)
            campaignType = EnumValues.campaign_type.regular;
         }
      }

      protected void cbUseSegments_CheckedChanged( object sender, EventArgs e )
      {
         if ( cbUseSegments.Checked )
            divSegmentOptions.Visible = true;
         else
            divSegmentOptions.Visible = false;
      }

      protected void ddlContent_type_SelectedIndexChanged( object sender, EventArgs e )
      {
         divContentHtml.Visible = false;
         divContentText.Visible = false;
         divContentUrl.Visible = false;
         divContentTemplate.Visible = false;
         divContentArchive.Visible = false;

         string content_type = ddlContent_type.SelectedValue;
         // ----------------------------------------------------
         switch ( content_type )
         {
            case "content":
               divContentHtml.Visible = true;
               divContentText.Visible = true;
               tbContentHtml.Focus();
               break;
            case "content_url":
               divContentUrl.Visible = true;
               divContentText.Visible = true;
               tbContentUrl.Focus();
               break;
            case "content_template":
               divContentTemplate.Visible = true;
               divContentText.Visible = true;
               break;
            case "content_archive":
               divContentArchive.Visible = true;
               tbContentArchive.Focus();
               break;
         }
      }

      private void SetupDefaults()
      {
         // reset to defaults
         cbAuthenticate.Checked = MCAPISettings.default_campaign_authenticate;
         cbInline_css.Checked = MCAPISettings.default_campaign_inline_css;
         cbAuto_footer.Checked = MCAPISettings.default_campaign_auto_footer;
         cbGenerate_text.Checked = MCAPISettings.default_campaign_generate_text;
         cbAuto_tweet.Checked = MCAPISettings.default_campaign_auto_tweet;
         cbTimeWarp.Checked = MCAPISettings.default_campaign_timewarp;
         cbOpens.Checked = MCAPISettings.default_campaign_tracking_opens;
         cbHtmlClicks.Checked = MCAPISettings.default_campaign_tracking_html_clicks;
         cbTextClicks.Checked = MCAPISettings.default_campaign_tracking_text_clicks;
         cbEcomm360.Checked = MCAPISettings.default_campaign_ecomm360;
         //
         cbUseSegments.Checked = false;
         tbSvcType.Text = ""; tbSvcTag.Text = "";

         // default to 'regular' & then o/r for specific type
         divAnalytics.Visible = true;

         ddlContent_type.SelectedValue = "content";
         divContentHtml.Visible = true;
         divContentText.Visible = true;
         divContentUrl.Visible = false;
         divContentTemplate.Visible = false;
         divContentArchive.Visible = false;

         divTypeOptsAuto.Visible = false;
         divTypeOptsRss.Visible = false;
         divTypeOptsABSplit.Visible = false;
         divSegmentOptions.Visible = false;
      }
      // ====================================================================
      private campaignCreateOptions campaignOptionsFormat()
      {
         int it;
         campaignCreateOptions options = new campaignCreateOptions();
         //
         options.list_id = tbListId.Text.Trim();
         int.TryParse( tbFolderId.Text.Trim(), out it );
         options.folder_id = it;
         options.subject = tbSubject.Text.Trim();
         options.from_email = tbFrom_email.Text.Trim();
         options.from_name = tbFrom_name.Text.Trim();
         options.to_email = tbTo_email.Text.Trim();
         if ( ddlContent_type.SelectedValue == "content_template" )
         {
            int.TryParse( tbTemplateId.Text.Trim(), out it );
            options.template_id = it;
         }
         else
         {
            options.template_id = 0;
         }
         options.tracking = new campaignTracking(
               cbOpens.Checked, cbHtmlClicks.Checked, cbTextClicks.Checked );
         options.title = tbTitle.Text.Trim();
         options.authenticate = cbAuthenticate.Checked;
         if ( !string.IsNullOrEmpty( tbSvcType.Text.Trim() ) )
         {
            options.analytics.Add( tbSvcType.Text.Trim(), tbSvcTag.Text.Trim() );
         }
         options.inline_css = cbInline_css.Checked;
         options.auto_footer = cbAuto_footer.Checked;
         options.generate_text = cbGenerate_text.Checked;
         options.auto_tweet = cbAuto_tweet.Checked;
         options.timewarp = cbTimeWarp.Checked;
         options.ecomm360 = cbEcomm360.Checked;
         //
         return options;
      }
      // --------------------------------------------------------------------------
      private Dictionary<string, string> campaignContentFormat()
      {
         Dictionary<string, string> content = new Dictionary<string, string>();
         //
         string content_type = ddlContent_type.SelectedValue;
         switch ( content_type )
         {
            case "content":
               if ( !string.IsNullOrEmpty( tbContentHtml.Text.Trim() ) )
               {
                  content.Add( "html", tbContentHtml.Text.Trim() );
               }
               if ( !string.IsNullOrEmpty( tbContentText.Text.Trim() ) )
               {
                  content.Add( "text", tbContentText.Text.Trim() );
               }
               break;
            case "content_url":
               if ( !string.IsNullOrEmpty( tbContentUrl.Text.Trim() ) )
               {
                  content.Add( "url", tbContentUrl.Text.Trim() );
               }
               if ( !string.IsNullOrEmpty( tbContentText.Text.Trim() ) )
               {
                  content.Add( "text", tbContentText.Text.Trim() );
               }
               break;
            case "content_template":
               Dictionary<string, string> template = sub_TemplateSections1.TemplateSections;
               foreach ( KeyValuePair<string, string> section in template )
               {
                  content.Add( section.Key, section.Value );
               }
               if ( !string.IsNullOrEmpty( tbContentText.Text.Trim() ) )
               {
                  content.Add( "text", tbContentText.Text.Trim() );
               }
               break;
            case "content_archive":
               throw new NotImplementedException( "I told you this was not implemented yet, right?" );
               break;
         }
         //
         return content;
      }
      // --------------------------------------------------------------------------
      private campaignSegmentOptions campaignSegmentOptsFormat()
      {
         campaignSegmentOptions segment_opts = new campaignSegmentOptions();
         //
         if ( cbUseSegments.Checked )
         {
            segment_opts.match = sub_SegmentOptions1.Match;
            segment_opts.conditions = sub_SegmentOptions1.SegmentConditions;
         }
         //
         return segment_opts;
      }
      // --------------------------------------------------------------------------
      private Dictionary<string, string> campaignTypeOptsFormat()
      {
         Dictionary<string, string> type_opts = new Dictionary<string, string>();

         if ( campaignType == EnumValues.campaign_type.absplit )
         {
            type_opts = getABSplitOptions();
         }
         else if ( campaignType == EnumValues.campaign_type.rss )
         {
            type_opts = sub_RssOptions1.type_opts;
         }
         else if ( campaignType == EnumValues.campaign_type.auto )
         {
            type_opts = getAutoResponderOptions();
         }
         //
         return type_opts;
      }

      // ====================================================================
      private Dictionary<string, string> getABSplitOptions()
      {
         Dictionary<string, string> options = new Dictionary<string, string>();
         for ( int i = 1; i <= 10; i++ )
         {
            switch ( i )
            {
               case 1:
                  if ( !string.IsNullOrEmpty( tbABSOpt01.Text.Trim() ) )
                     options.Add( tbABSOpt01.Text.Trim(), tbABSVal01.Text.Trim() );
                  break;
               case 2:
                  if ( !string.IsNullOrEmpty( tbABSOpt02.Text.Trim() ) )
                     options.Add( tbABSOpt02.Text.Trim(), tbABSVal02.Text.Trim() );
                  break;
               case 3:
                  if ( !string.IsNullOrEmpty( tbABSOpt03.Text.Trim() ) )
                     options.Add( tbABSOpt03.Text.Trim(), tbABSVal03.Text.Trim() );
                  break;
               case 4:
                  if ( !string.IsNullOrEmpty( tbABSOpt04.Text.Trim() ) )
                     options.Add( tbABSOpt04.Text.Trim(), tbABSVal04.Text.Trim() );
                  break;
               case 5:
                  if ( !string.IsNullOrEmpty( tbABSOpt05.Text.Trim() ) )
                     options.Add( tbABSOpt05.Text.Trim(), tbABSVal05.Text.Trim() );
                  break;
               case 6:
                  if ( !string.IsNullOrEmpty( tbABSOpt06.Text.Trim() ) )
                     options.Add( tbABSOpt06.Text.Trim(), tbABSVal06.Text.Trim() );
                  break;
               case 7:
                  if ( !string.IsNullOrEmpty( tbABSOpt07.Text.Trim() ) )
                     options.Add( tbABSOpt07.Text.Trim(), tbABSVal07.Text.Trim() );
                  break;
               case 8:
                  if ( !string.IsNullOrEmpty( tbABSOpt08.Text.Trim() ) )
                     options.Add( tbABSOpt08.Text.Trim(), tbABSVal08.Text.Trim() );
                  break;
               case 9:
                  if ( !string.IsNullOrEmpty( tbABSOpt09.Text.Trim() ) )
                     options.Add( tbABSOpt09.Text.Trim(), tbABSVal09.Text.Trim() );
                  break;
               case 10:
                  if ( !string.IsNullOrEmpty( tbABSOpt10.Text.Trim() ) )
                     options.Add( tbABSOpt10.Text.Trim(), tbABSVal10.Text.Trim() );
                  break;
            }
         }
         return options;
      }
      // --------------------------------------------------------------------------------------
      private Dictionary<string, string> getAutoResponderOptions()
      {
         Dictionary<string, string> options = new Dictionary<string, string>();
         options.Add( lblAutoUnitsKey.Text, ddlAutoUnitsVal.SelectedValue );
         options.Add( lblAutoTimeKey.Text, tbAutoTimeVal.Text.Trim() );
         options.Add( lblAutoDirKey.Text, ddlAutoDirVal.SelectedValue );
         options.Add( lblAutoEventKey.Text, ddlAutoEventVal.SelectedValue );
         if ( !string.IsNullOrEmpty( tbAutoMergeVal.Text.Trim() ) )
         { options.Add( lblAutoMergeKey.Text, tbAutoMergeVal.Text.Trim() ); }
         //
         return options;
      }
   }
}